package com.ywjt.pinkelephant.assistant;

import android.content.Context;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.cy.tablayoutniubility.TabLayoutScroll;
import com.google.android.material.tabs.TabLayout;
import com.stx.xhb.xbanner.XBanner;
import com.ywjt.pinkelephant.R;
import com.ywjt.pinkelephant.assistant.adapter.AssistantMidAdapter;
import com.ywjt.pinkelephant.assistant.fragment.AssistantFragment;
import com.ywjt.pinkelephant.base.BaseVPFragment;
import com.ywjt.pinkelephant.widget.ViewPagerForScrollView;

import java.util.ArrayList;
import java.util.List;


public class AssistantIndex extends BaseVPFragment implements View.OnClickListener {
    private Context context;
    private View view;
    private XBanner banner;
    private List<Integer> banners;
    private AssistantMidAdapter  assistantMidAdapter;
    private RecyclerView rcCommon;
    private TabLayoutScroll tab1;
    private int status=0;//0 已领取 1 已过期
    private ViewPagerForScrollView viewpager;
    private TabLayout tabLayout;
    private List<Fragment> fragmentList;
    private MainAdapter mainAdapter;
    private List<String> mTitleList;



    @Override
    public void onClick(View v) {
        switch (v.getId()){

        }
    }

    @Override
    protected void onFragmentVisibleChange(boolean isVisible) { }

    @Override
    protected void onFragmentFirstVisible() { }

    public static AssistantIndex newInstance(String content) {
        Bundle arguments = new Bundle();
        arguments.putString("string", content);
     AssistantIndex fl = new AssistantIndex();
        fl.setArguments(arguments);
        return fl;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.assistant_index, container, false);
            context = getActivity();
            initView();
        }
        return view;
    }

    public void initView(){
        tabLayout=view.findViewById(R.id.tabLayout);
        banner=view.findViewById(R.id.banner);
        rcCommon=view.findViewById(R.id.rcCollect);
        tab1=view.findViewById(R.id.top_bar);
        viewpager=view.findViewById(R.id.viewpager);
//        FragPageAdapterVp2<String> fragmentPageAdapter = new FragPageAdapterVp2<String>(this) {
//
//            @Override
//            public Fragment createFragment(String bean, int position) {
//                return AssistantFragment.newInstance(status);
//
//            }
//
//            @Override
//            public void bindDataToTab(TabViewHolder holder, int position, String bean, boolean isSelected) {
//                TextView textView = holder.getView(R.id.tv);
//                if (isSelected) {
//                    textView.setTextColor(0xff333333);
//                    textView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
//                } else {
//                    textView.setTextColor(0xff666666);
//                    textView.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
//                }
//                textView.setText(bean);
//            }
//
//
//            @Override
//            public int getTabLayoutID(int position, String bean) {
//                return R.layout.tab_item;
//            }
//        };
//        TabAdapter<String> tabAdapter = new TabMediatorVp2<String>(tab1, viewpager).setAdapter(fragmentPageAdapter);

        mTitleList=new ArrayList<>();
        mTitleList.add("全部");
        mTitleList.add("大咖精英");
        mTitleList.add("干货教程");
        mTitleList.add("推广教程");

        fragmentList=new ArrayList<>();
        fragmentList.add(new AssistantFragment(context,viewpager,0));
        fragmentList.add(new AssistantFragment(context,viewpager,1));
        fragmentList.add(new AssistantFragment(context,viewpager,2));
        fragmentList.add(new AssistantFragment(context,viewpager,3));


        mainAdapter=new MainAdapter(getChildFragmentManager());
        tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        tabLayout.setupWithViewPager(viewpager);
        viewpager.setAdapter(mainAdapter);
        viewpager.resetHeight(0);
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewpager.resetHeight(tab.getPosition());
                viewpager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) { }

            @Override
            public void onTabReselected(TabLayout.Tab tab) { }
        });

        banners=new ArrayList<>();
        banners.add(R.mipmap.icon_banner);
        banners.add(R.mipmap.icon_banner);
        banner.setData(R.layout.item_banner1,banners,null);
        banner.loadImage(new XBanner.XBannerAdapter() {
            @Override
            public void loadBanner(XBanner banner, Object object, View view, int position) {
                RelativeLayout container=(RelativeLayout) view;
                ImageView ivBanner=container.findViewById(R.id.ivBanner);
                Glide.with(context).
                        load(banners.get(position))
                        .apply(new RequestOptions().transforms(new CenterCrop(), new RoundedCorners(10)))
                        .into(ivBanner);
            }
        });

        assistantMidAdapter=new AssistantMidAdapter(banners);
        rcCommon.setAdapter(assistantMidAdapter);
        GridLayoutManager manager=new GridLayoutManager(context,2,RecyclerView.VERTICAL,false);
        rcCommon.setLayoutManager(manager);
    }


    //适配器
    public class MainAdapter extends FragmentPagerAdapter {

        public MainAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return fragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mTitleList.size();
        }

        //获取标题
        @Override
        public CharSequence getPageTitle(int position) {
            return mTitleList.get(position);
        }
    }


}